package ano.it.service.impl;

import ano.it.dao.MenuDao;
import ano.it.dao.RoleDao;
import ano.it.dao.UserDao;
import ano.it.pojo.Menu;
import ano.it.pojo.Role;
import ano.it.pojo.User;
import ano.it.service.MenuService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Override
    public List<Menu> findByMenu(String username) {
        User user = userDao.findByUsername(username);
        Set<Role> roles = roleDao.findByUserId(user.getId());
        List<Menu> menus=null;
        for (Role role : roles) {
            menus=new ArrayList<>();
            menus=menuDao.findByRoleId(role.getId());
        }
        for (Menu menu : menus) {
                List<Menu> children=menuDao.findByChildren(menu.getId());
                menu.setChildren(children);
        }
        return menus;
    }
}
